package com.sebklebes.radar.radar;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.sebklebes.radar.R;

import java.util.LinkedList;
import java.util.List;

/**
 * ListAdapter for Channel
 *
 * @author voicu.klein
 */
public class MyAdapter implements ListAdapter {

	private static final String TAG = MyAdapter.class.getName();
	private List<String> list = new LinkedList<String>();
	private Context context;

	public MyAdapter(List<String> list, Context context) {
		this.list = list;
		this.context = context;
	}

	/**
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return list.size();
	}

	/**
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	/**
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return 0;
	}

	/**
	 * @see android.widget.Adapter#getItemViewType(int)
	 */
	@Override
	public int getItemViewType(int position) {
		return ListAdapter.IGNORE_ITEM_VIEW_TYPE;
	}

	/**
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 *      android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View arg1, ViewGroup parent) {

		final LayoutInflater inflater = (LayoutInflater) parent.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View view = inflater.inflate(R.layout.list_layout, null);

		TextView settingName = (TextView) view.findViewById(R.id.settingName);
		final String name = (String) getItem(position);
		settingName.setText(name);
		final CheckBox activeCheckBox = (CheckBox) view
				.findViewById(R.id.checkBox);
		// copii, nu faceti asa ceva acasa, da is obosit si mi lene sa gasesc
		// alta solutie :))

		switch (position) {
			case 0: {
				activeCheckBox.setChecked(Preferences.getInstance(context)
						.isRealTimeRadars());
				activeCheckBox.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Preferences.getInstance(context).saveRealTimeRadars(
								activeCheckBox.isChecked());
					}
				});
				break;
			}
			case 1: {
				activeCheckBox.setChecked(Preferences.getInstance(context)
						.isKnownRadars());
				activeCheckBox.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Preferences.getInstance(context).saveKnownRadars(
								activeCheckBox.isChecked());
					}
				});
				break;
			}
			case 2: {
				activeCheckBox.setChecked(Preferences.getInstance(context)
						.isSpeedCams());
				activeCheckBox.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Preferences.getInstance(context).saveSpeedCams(
								activeCheckBox.isChecked());
					}
				});
				break;
			}
		}

		return view;
	}

	/**
	 * @see android.widget.Adapter#getViewTypeCount()
	 */
	@Override
	public int getViewTypeCount() {
		return 1;
	}

	/**
	 * @see android.widget.Adapter#hasStableIds()
	 */
	@Override
	public boolean hasStableIds() {
		return true;
	}

	/**
	 * @see android.widget.Adapter#isEmpty()
	 */
	@Override
	public boolean isEmpty() {
		return list.size() > 0;
	}

	/**
	 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
	 */
	@Override
	public void registerDataSetObserver(DataSetObserver observer) {

	}

	/**
	 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
	 */
	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	/**
	 * @see android.widget.ListAdapter#areAllItemsEnabled()
	 */
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	/**
	 * @see android.widget.ListAdapter#isEnabled(int)
	 */
	@Override
	public boolean isEnabled(int arg0) {
		return true;
	}
}
